import React from 'react';
import {Text, StyleSheet, View} from 'react-native';

const LongText = ({text, maxLength, style}) => {
  // 计算省略号前后应该保留的字符数
  const ellipsisLength = 3; // 省略号的长度
  const preEllipsisLength = Math.floor((maxLength - ellipsisLength) / 2);
  const postEllipsisLength = maxLength - ellipsisLength - preEllipsisLength;

  // 截取文字并添加省略号
  const truncatedText =
    text.length > maxLength
      ? `${text.slice(0, preEllipsisLength)}...${text.slice(
          -postEllipsisLength,
        )}`
      : text;

  return (
    <View style={styles.container}>
      <Text style={style}>{truncatedText}</Text>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
});
export default LongText;
